﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace VRB_0._1
{
    class Avatar : AnimatedObject
    {
        public enum Action
        {
            MOVING, STANDING, JUMPING, LANDING, DYING
        }

        public Animation StandingLeftAnimation, StandingRightAnimation, MovingLeftAnimation, MovingRightAnimation, JumpingLeftAnimation, JumpingRightAnimation, DyingAnimation;

        public Action action = Action.STANDING;
        public Action lastAction;
        public Direction direction = Direction.RIGHT;


        public float MoveSpeed = 4.0f;
        public float currentJumpSpeed = 0.0f;
        public const float MAXJUMPSPEED = 35.0f;

        public Vector2 lastPosition;

        public void Initialize(Viewport Viewport, ContentManager Content, Vector2 position, AnimatedObject.Direction Startdirection)
        {
            StandingLeftAnimation = new Animation();
            Texture2D StandingLeftTexture = Content.Load<Texture2D>("avatar_left");
            StandingLeftAnimation.Initialize(StandingLeftTexture, position, 54, 87, 72, 50, 166, 1, 600, Color.White, 1f, true, true);

            StandingRightAnimation = new Animation();
            Texture2D StandingRightTexture = Content.Load<Texture2D>("avatar_right");
            StandingRightAnimation.Initialize(StandingRightTexture, position, 60, 90, 0, 0, 0, 1, 600, Color.White, 1f, true, true);

            MovingLeftAnimation = new Animation();
            Texture2D MovingLeftTexture = Content.Load<Texture2D>("avatar_left");
            MovingLeftAnimation.Initialize(MovingLeftTexture, position, 54, 87, 72, 50, 166, 3, 60, Color.White, 1f, true, true);

            MovingRightAnimation = new Animation();
            Texture2D MovingRightTexture = Content.Load<Texture2D>("avatar_right");
            MovingRightAnimation.Initialize(MovingRightTexture, position, 60, 90, 0, 0, 0, 3, 60, Color.White, 1f, true, true);

            JumpingLeftAnimation = new Animation();
            Texture2D JumpingLeftTexture = Content.Load<Texture2D>("avatar_left");
            JumpingLeftAnimation.Initialize(JumpingLeftTexture, position, 54, 87, 512, 50, 166, 1, 600, Color.White, 1f, true, true);

            JumpingRightAnimation = new Animation();
            Texture2D JumpingRightTexture = Content.Load<Texture2D>("avatar_right");
            JumpingRightAnimation.Initialize(JumpingRightTexture, position, 60, 90, 120, 0, 0, 1, 600, Color.White, 1f, true, true);

            DyingAnimation = new Animation();
            Texture2D DyingTexture = Content.Load<Texture2D>("explosion");
            DyingAnimation.Initialize(DyingTexture, position, 134, 134, 0, 0, 0, 12, 45, Color.White, 1f, false, true);

            lastPosition = StartPosition;

            base.Initialize(Viewport, MovingLeftAnimation, position, Startdirection);
        }

        public override void Update(GameTime gameTime)
        {
            lastPosition = Position;
            lastAction = action;

            if (direction == Direction.LEFT)
            {
                switch (action)
                {
                    case Action.STANDING:
                        StandingLeftAnimation.Position = Position;
                        StandingLeftAnimation.Update(gameTime);
                        break;
                    case Action.MOVING:
                        MovingLeftAnimation.Position = Position;
                        MovingLeftAnimation.Update(gameTime);
                        break;
                    case Action.JUMPING:
                        JumpingLeftAnimation.Position = Position;
                        JumpingLeftAnimation.Update(gameTime);
                        break;
                    case Action.LANDING:
                        JumpingLeftAnimation.Position = Position;
                        JumpingLeftAnimation.Update(gameTime);
                        break;
                    case Action.DYING:
                        DyingAnimation.Position = Position - new Vector2(67 - this.Width / 2, 67 - this.Height / 2);
                        DyingAnimation.Update(gameTime);
                        break;
                }
            }
            else //Direction is Direction.RIGHT
            {
                switch (action)
                {
                    case Action.STANDING:
                        StandingRightAnimation.Position = Position;
                        StandingRightAnimation.Update(gameTime);
                        break;
                    case Action.MOVING:
                        MovingRightAnimation.Position = Position;
                        MovingRightAnimation.Update(gameTime);
                        break;
                    case Action.JUMPING:
                        JumpingRightAnimation.Position = Position;
                        JumpingRightAnimation.Update(gameTime);
                        break;
                    case Action.LANDING:
                        JumpingRightAnimation.Position = Position;
                        JumpingRightAnimation.Update(gameTime);
                        break;
                    case Action.DYING:
                        DyingAnimation.Position = Position - new Vector2(67 - this.Width / 2, 67 - this.Height / 2);
                        DyingAnimation.Update(gameTime);
                        break;
                }
            }
        }
        public override void Draw(SpriteBatch spriteBatch)
        {
            if (direction == Direction.LEFT)
            {
                switch (action)
                {
                    case Action.STANDING:
                        StandingLeftAnimation.Draw(spriteBatch);
                        break;
                    case Action.MOVING:
                        MovingLeftAnimation.Draw(spriteBatch);
                        break;
                    case Action.JUMPING:
                        JumpingLeftAnimation.Draw(spriteBatch);
                        break;
                    case Action.LANDING:
                        JumpingLeftAnimation.Draw(spriteBatch);
                        break;
                    case Action.DYING:
                        DyingAnimation.Draw(spriteBatch);
                        break;
                }
            }
            else //Direction is Direction.RIGHT
            {
                switch (action)
                {
                    case Action.STANDING:
                        StandingRightAnimation.Draw(spriteBatch);
                        break;
                    case Action.MOVING:
                        MovingRightAnimation.Draw(spriteBatch);
                        break;
                    case Action.JUMPING:
                        JumpingRightAnimation.Draw(spriteBatch);
                        break;
                    case Action.LANDING:
                        JumpingRightAnimation.Draw(spriteBatch);
                        break;
                    case Action.DYING:
                        DyingAnimation.Draw(spriteBatch);
                        break;
                }
            }
        }

        public void InputLeft()
        {
            direction = Direction.LEFT;


            Position.X -= MoveSpeed;

            Position.X = MathHelper.Clamp(Position.X, 0, viewport.Width - Width);
            if (lastPosition.Y >= Position.Y && action == Action.JUMPING)
                action = Action.JUMPING;
            else
                action = Action.MOVING;
        }
        public void InputRight()
        {
            direction = Direction.RIGHT;

            Position.X += MoveSpeed;

            Position.X = MathHelper.Clamp(Position.X, 0, viewport.Width - Width);
            if (lastPosition.Y >= Position.Y && action == Action.JUMPING)
                action = Action.JUMPING;
            else
                action = Action.MOVING;
        }
        public void InputUp()
        {

        }
        public void InputDown()
        {

        }
        public void InputJump()
        {
            if (action == Action.LANDING)
                return;

            if (lastAction != Action.JUMPING)
                currentJumpSpeed = MAXJUMPSPEED;

            action = Action.JUMPING;
            //Position.Y -= currentJumpSpeed;
            currentJumpSpeed -= 2.0f;

            if (lastAction == Action.JUMPING && currentJumpSpeed <= 0)
                action = Action.LANDING;
        }

        public void InputNothing()
        {
            action = Action.STANDING;
        }
        public void die()
        {
            action = Action.DYING;
        }
    }
}
